/*
 * Yet Another Haxe Library, Copyright (c) 2009, Jean-Philippe CIMAN (Jeanffy)
 * All rights reserved.
 * Permission is hereby granted to use, copy, modify, and distribute this
 * source code, or portions hereof, for any purpose, without fee, subject
 * to the following restrictions:
 *
 * 1. The origin of this source code must not be misrepresented.
 * 2. Altered versions must be plainly marked as such and
 *    must not be misrepresented as being the original source.
 * 3. This copyright notice may not be removed or altered from
 *    any source or altered source distribution.
 *
 * If you use this source code in a product, acknowledgment is not
 * required but would be appreciated.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE PROJECT AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#if flash9
import flash.display.MovieClip;
import flash.geom.Rectangle;
#end

/**
	This class provides some basic mathematical computation
	and also some tools
**/
class YahStd
{
	/** Converts degrees to radians **/
	public inline static function toRadians(degrees:Float):Float
	{
		return degrees*0.017453292519943295769236907684886;
	}

	/** Cconverts radians to degrees **/
	public inline static function toDegrees(radians:Float):Float
	{
		return radians*57.295779513082320876798154814105;
	}
	
	/** Returns the distance between point (x1,y1) and point (x2,y2) **/
	public inline static function ptptDistance(x1:Float, y1:Float, x2:Float, y2:Float):Float
	{
		var dx:Float = x1 - x2;
		var dy:Float = y1 - y2;
		return Math.sqrt(dx*dx + dy*dy);
	}

	/** Returns the squared distance between point (x1,y1) and point (x2,y2) **/
	public inline static function ptptDistance2(x1:Float, y1:Float, x2:Float, y2:Float):Float
	{
		var dx:Float = x1 - x2;
		var dy:Float = y1 - y2;
		return dx*dx + dy*dy;
	}

	/** Returns a random integer number >= min and <= max **/
	public inline static function random(min:Int, max:Int):Int
	{
		return Math.round(Math.random() * (max-min)) + min;
	}
	
	/** Returns a random floating point number >= min and <= max **/
	public inline static function frandom(min:Float, max:Float):Float
	{
		return Math.round(Math.random() * (max-min)) + min;
	}

	/** Returns true if the point (x,y) is inside the circle center (cx,cy) and radius radius **/
	public inline static function ptInCircle(x:Float, y:Float, cx:Float, cy:Float, radius:Float):Bool
	{
		return ptptDistance2(x, y, cx, cy) < (radius*radius);
	}

	/** Returns the degrees angle between the segment (x1,y1), (x2,y2) and the x+ axis **/
	public inline static function angleDegrees(x1:Float, y1:Float, x2:Float, y2:Float):Float
	{
		return toDegrees(Math.atan2(y2-y1, x2-x1));
	}
	
	/** Returns true if a random integer number between 0 and 100 is less than num **/
	public inline static function percent(num:Int) : Bool
	{
		return ((Math.round(Math.random() * 100)) < num);
	}
	
	#if debug
	public inline static function assert(condition:Bool, msg:String)
	{
		if (!condition)
			trace("ASSERT: " + msg);
	}
	#end
	
#if flash8
	/**
		Trying to avoid link to game from other websites<br/>
		Returns true if game is authorized, false if not.<br/>
		domain should be of the form 'www.domain.com'<br/>
		TODO: see if really useful and correct
	**/
	public static function licence(domain:String):Bool
	{
		var p:String = flash.Lib._root._url;
		var q:String = "";
		var i:Int, s:Int = p.length, k:Int = 0, c:String;
		for (i in 7...s)
		{
			c = p.charAt(i);
			if (c != '/')
			{
				q += c;
				k++;
			}
			else
				break;
		}
		if (q != domain)
		{
			var licText:flash.TextField = flash.Lib._root.createTextField("licence", 100000, 0, 0, 400, 100);

			var format:flash.TextFormat = new flash.TextFormat();
			format.color = 0x000000;
			format.size = 20;

			licText.selectable = false;
			licText.setNewTextFormat(format);
			licText.text = "Play this game on\nhttp://"+domain;
			
			return false;
		}
		
		return true;
	}
#end

#if flash9
	/** I wish I could **/
	public static function duplicateMovieClip(clip:MovieClip):MovieClip
	{		
		return clip;
	}
#end
}
